import os
from util.requestOwn import my_request
from celery.task import task

from apps.manageFile.models import MFileModel
from apps.delay_missions import *
from django.conf import settings
from apps.missions.models import Mission
from apps.gateway.models import Sensor, Gateway
from util.md5 import getmd5

UPDATE_GATEWAY = 1
UPDATE_SENSOR = 2
DOWN_SCRIPT = 3
DOWN_DELPLOY = 4
AGREEMENT = 5
MAIN = 6
CONFIG = 7


def cacultor_md5(file_type, path):
    if file_type == AGREEMENT or file_type == MAIN or file_type == CONFIG:
        is_new = "1"
    else:
        is_new = "0"
    result = my_request.get_md5(path, is_new)
    md5 = result.get("md5")
    if md5:
        return path, md5
    else:
        return False, False


@task(name="new_gateway_config_task")
def config_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    file_path, md5 = cacultor_md5(CONFIG, file_name)
    if file_path:
        data["mission"] = mission
        data["now_tid"] = data.get("now_tid")
        data["is_pub"] = data.get("is_pub")
        data["is_again"] = True
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["mission"] = mission
        data["file_path"] = file_path
        down_deploy = DownDeploy(data)
        down_deploy.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()
    mission.save()


@task(name="new_gateway_main_task")
def main_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    file_path, md5 = cacultor_md5(MAIN, file_name)
    if file_path:
        data["mission"] = mission
        data["is_again"] = True
        data["is_pub"] = data.get("is_pub")
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["file_path"] = file_path
        data["now_tid"] = data.get("now_tid")
        main_gateway = MainGateway(data)
        main_gateway.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="new_gateway_read_gateway_task")
def read_gateway_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    data["mission"] = mission
    data["is_again"] = True
    data["is_pub"] = data.get("is_pub")
    data["gateway_client_id"] = gateway.client_id
    data["gateway_id"] = gateway.gateway_id
    data["is_tac31"] = gateway.is_tac31
    data["now_tid"] = data.get("now_tid")
    read_gateway = ReadGateway(data)
    read_gateway.run()
    read_string = mission.mission_result
    if read_string:
        read_strings = read_string.split("+")
        singal = read_strings[4]
        gateway.singal = int(singal)
        gateway.save()


@task(name="new_gateway_agreement_task")
def agreement_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    file_path, md5 = cacultor_md5(AGREEMENT, file_name)
    if file_path:
        data["mission"] = mission
        data["is_pub"] = data.get("is_pub")
        data["is_again"] = True
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["file_path"] = file_path
        data["now_tid"] = data.get("now_tid")
        agreementGateway = AgreementGateway(data)
        agreementGateway.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="new_gateway_reboot_gateway_task")
def reboot_gateway_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    data["mission"] = mission
    data["is_again"] = True
    data["gateway_client_id"] = gateway.client_id
    data["is_pub"] = data.get("is_pub")
    data["gateway_id"] = gateway.gateway_id
    data["is_tac31"] = gateway.is_tac31
    data["now_tid"] = data.get("now_tid")
    reboot_gateway = RebootGateway(data)
    reboot_gateway.run()


@task(name="new_gateway_poweroff_task")
def poweroff_gateway_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    data["mission"] = mission
    data["now_tid"] = data.get("now_tid")
    data["is_again"] = True
    data["is_pub"] = data.get("is_pub")
    data["gateway_client_id"] = gateway.client_id
    data["gateway_id"] = gateway.gateway_id
    data["is_tac31"] = gateway.is_tac31
    poweroff = PowerOff(data)
    poweroff.run()
